package exam.wangyi.autumn.S4;

import java.util.*;

public class Main {
    public static int n;
    public static int[] nums;
    public static Map<Integer, List<Integer>> map = new HashMap<>();

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        nums = new int[n];
        for (int i=0; i<n; ++i)
            nums[i] = sc.nextInt();
        System.out.println(solution());
    }

    private static int solution() {
        for (int i=0; i<n; ++i) {
            if (!map.containsKey(nums[i]))
                map.put(nums[i], new ArrayList<>());
            map.get(nums[i]).add(i);
        }
        Map<Integer, List<Integer>> res = new HashMap<>();
        for (int i=0; i<n; ++i) {
            List<Integer> list = map.get(nums[i]);
            if (list.size() == 1 || list.get(0) != i)
                continue;
            res.put(nums[i], new ArrayList<>());
            for (int j=0; j<list.size()-1; ++j) {
                int start = list.get(j), end = list.get(j+1);
                res.get(nums[i]).add(getNum(start, end, nums[i]));
            }
        }
        int result = 0;
        for (Integer key : res.keySet()) {
            List<Integer> list = res.get(key);
            for (int i=0; i<list.size(); ++i) {
                int sum = 0;
                for (int j=i; j<list.size(); ++j) {
                    for (int k=i; k<=j; ++k)
                        sum += list.get(k);
                }
                result += sum;
            }
        }
        return result;
    }

    private static int getNum(int start, int end, int border) {
        int res = 0;
        for (int i=start+1; i<end; ++i) {
            if (nums[i] < border)
                res++;
        }
        return res;
    }
}